import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../models/product.dart';
import '../providers/favorites_provider.dart';

class DetailPage extends StatelessWidget {
  const DetailPage({super.key});

  @override
  Widget build(BuildContext context) {
    final Product product = ModalRoute.of(context)!.settings.arguments as Product;
    final favs = context.watch<FavoritesProvider>();
    final isFav = favs.isFav(product.id);
    final img = product.images.isNotEmpty ? product.images.first : product.thumbnail;
    return Scaffold(
      appBar: AppBar(title: const Text('详情')),
      floatingActionButton: AnimatedScale(
        scale: isFav ? 1.2 : 1.0,
        duration: const Duration(milliseconds: 240),
        child: FloatingActionButton(
          onPressed: () => favs.toggle(product),
          child: Icon(isFav ? Icons.favorite : Icons.favorite_border),
        ),
      ),
      body: ListView(
        padding: const EdgeInsets.all(16),
        children: [
          AspectRatio(
            aspectRatio: 16 / 9,
            child: ClipRRect(
              borderRadius: BorderRadius.circular(12),
              child: Image.network(
                img,
                fit: BoxFit.cover,
                errorBuilder: (c, e, s) => Container(color: Colors.grey.shade300, child: const Icon(Icons.broken_image)),
              ),
            ),
          ),
          const SizedBox(height: 12),
          Text(product.title, style: Theme.of(context).textTheme.titleLarge),
          const SizedBox(height: 8),
          Wrap(spacing: 8, runSpacing: -8, children: [
            Chip(label: Text(product.category)),
            if (product.brand.isNotEmpty) Chip(label: Text(product.brand)),
          ]),
          const SizedBox(height: 12),
          Text(product.description),
        ],
      ),
    );
  }
}